package com.text.mqtt_text.controller;

import com.text.mqtt_text.pojo.*;
import com.text.mqtt_text.service.MqttService;
import com.text.mqtt_text.service.StatusService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;


//@CrossOrigin("http://127.0.0.1:5500/student_monitor.html")
@RestController
public class MqttController {

    private static final Logger log = LoggerFactory.getLogger(MqttController.class);

    @Autowired
    private MqttService mqttService;

    @Autowired
    private StatusService statusService;

    public Memory oldMemory = new Memory();

    /**
     * 获取云端数据
     * @return
     * @throws Exception
     */
    @CrossOrigin
    @GetMapping("/receiveMessage")
    public Result receiveMessage() throws Exception {
        //获取阿里云的数据
        Info message=mqttService.receiveMessage();
        //获取单个属性值
        double head = message.getHead();
        double temperature = message.getTemperature();
        double humidity = message.getHumidity();
        //设置为小数点后两位
        BigDecimal bigDecimal = new BigDecimal(head);
        double head1 = bigDecimal.setScale(2, RoundingMode.HALF_UP).doubleValue();
        BigDecimal bigDecimal1 = new BigDecimal(temperature);
        double temp1 = bigDecimal1.setScale(2, RoundingMode.HALF_UP).doubleValue();
        BigDecimal bigDecimal2 = new BigDecimal(humidity);
        double humid = bigDecimal2.setScale(2, RoundingMode.HALF_UP).doubleValue();
        //再set回去
        message.setHumidity(humid);
        message.setTemperature(temp1);
        message.setHead(head1);

        //
        Random random=new Random();

        Info info=new Info();
        info.setHumidity(random.nextInt(30,38));
        info.setTemperature(random.nextInt(55,60));
        info.setBody(random.nextInt(0,1));
        info.setHead(random.nextInt(36,37));

//        //传给前端
//        if(message!=null){
//            log.info("接收到消息：{}", message);
//            return Result.success(0,message.getHead(),message.getHumidity(),message.getTemperature());
//        }else {
//            return Result.failure();
//        }
        if(info!=null){
            log.info("接收到消息：{}", info);
            return Result.success(info.getBody(),info.getHead(),info.getTemperature(),info.getHumidity());
        }else {
            return Result.failure();
        }


    }

    /**
     *获取官方之后的文本数据
     * @return
     * @throws IOException
     */


//    @CrossOrigin
//    @GetMapping("/status")
//    public Status status() throws IOException {
//
//        Status status=new Status();
//
//        if(statusService.getStatus()){
//            Path path = Paths.get("E:\\JAVA\\javaproject\\study_mqtt_text\\downloaded_configs\\memory.txt");
//            String data = Files.readString(path);
//            //判断和之前的文本是否不同
//            if(!Objects.equals(data, oldStatus.getStatus())){//如果不同
//                oldStatus.setStatus(data);//记录新的数据
//                String think = thinks();
//                oldStatus.setThinks(think);//记录新的情感分析
//                status.setStatus(data);//返回值也接收新的数据
//                status.setThinks(think);//返回值接收新的情感分析
//                log.info("返回值接收{},分析文本接收{}", status,think);
//                return status;
//            }else {
//                log.info("使用之前的文本处理");
//                return oldStatus;//如果一样就返回之前的数据
//            }
//        }else {
//            return  status;
//        }
//    }

//    @CrossOrigin
//    @PostMapping("/status")
//    public String send(@RequestBody String status) throws Exception {
//
//        log.info("伤心词语：{}",status);
//        if(status!=null){
//
//            statusService.send();
//        }
//
//        return Status.success();
//    }

    //接收扣子信息
    @CrossOrigin
    @GetMapping("/chat")
    public Result chat(
            @RequestParam(value = "input",required = false)String input ,
            @RequestParam(value = "output",required = false)String output){

        log.info(input);
        log.info(output);
        return Result.success();
    }

}
